import { server } from '../config/server';
import { useToken } from '../utils/token';

export const focuscard = {
    /*
     * 专注模式打卡
     */
    focuscard(duration) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/focuscard/focuscard`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { duration },
                success: (res) => {
                    console.log('【 Response in request focuscard 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request focuscard 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    checkinStatistics() {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/focuscard/checkinStatistics`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request checkinStatistics 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request checkinStatistics 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    checkinRanking() {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/focuscard/checkinRanking`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request checkinRanking 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request checkinRanking 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    checkinTrend() {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/focuscard/checkinTrend`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request checkinTrend 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request checkinTrend 】', err);
                    reject({ success: false, err });
                }
            });
        });
    }
};